﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:SysBasicCodeName
    /// </summary>
    public partial class SysBasicCodeName
    {
        public SysBasicCodeName()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
       /// <param name="fCodeClsNo"></param>
       /// <param name="fEntityNo"></param>
       /// <param name="fCodeNo"></param>
       /// <returns></returns>
        public bool Exists(string fCodeClsNo, string fEntityNo, string fCodeNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_SysBasicCodeName");
            strSql.Append(" where FCodeClsNo=@FCodeClsNo and FEntityNo=@FEntityNo and FCodeNo=@FCodeNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FCodeClsNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FCodeNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fCodeClsNo;
            parameters[1].Value = fEntityNo;
            parameters[2].Value = fCodeNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model"></param>
        public void Add(ElingOA6_Admin_DSJCJ.Model.SysBasicCodeName model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_SysBasicCodeName(");
            strSql.Append("FCodeClsNo,FEntityNo,FCodeNo,FCodeName,FDataType)");
            strSql.Append(" values (");
            strSql.Append("@FCodeClsNo,@FEntityNo,@FCodeNo,@FCodeName,@FDataType)");
            SqlParameter[] parameters = {
					new SqlParameter("@FCodeClsNo", SqlDbType.NVarChar,8),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FCodeNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FCodeName", SqlDbType.NVarChar,60),
					new SqlParameter("@FDataType", SqlDbType.Char,1)};
            parameters[0].Value = model.FCodeClsNo;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FCodeNo;
            parameters[3].Value = model.FCodeName;
            parameters[4].Value = model.FDataType;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.SysBasicCodeName model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_SysBasicCodeName set ");
            strSql.Append("FCodeName=@FCodeName,");
            strSql.Append("FDataType=@FDataType");
            strSql.Append(" where FCodeClsNo=@FCodeClsNo and FEntityNo=@FEntityNo and FCodeNo=@FCodeNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FCodeName", SqlDbType.NVarChar,60),
					new SqlParameter("@FDataType", SqlDbType.Char,1),
					new SqlParameter("@FCodeClsNo", SqlDbType.NVarChar,8),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FCodeNo", SqlDbType.NVarChar,25)};
            parameters[0].Value = model.FCodeName;
            parameters[1].Value = model.FDataType;
            parameters[2].Value = model.FCodeClsNo;
            parameters[3].Value = model.FEntityNo;
            parameters[4].Value = model.FCodeNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="fCodeClsNo"></param>
        /// <param name="fEntityNo"></param>
        /// <param name="fCodeNo"></param>
        /// <returns></returns>
        public bool Delete(string fCodeClsNo, string fEntityNo, string fCodeNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_SysBasicCodeName ");
            strSql.Append(" where FCodeClsNo=@FCodeClsNo and FEntityNo=@FEntityNo and FCodeNo=@FCodeNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FCodeClsNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FCodeNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fCodeClsNo;
            parameters[1].Value = fEntityNo;
            parameters[2].Value = fCodeNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
       /// <param name="fCodeClsNo"></param>
       /// <param name="fEntityNo"></param>
       /// <param name="fCodeNo"></param>
       /// <returns></returns>
        public ElingOA6_Admin_DSJCJ.Model.SysBasicCodeName GetModel(string fCodeClsNo, string fEntityNo, string fCodeNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FCodeClsNo,FEntityNo,FCodeNo,FCodeName,FDataType from t_SysBasicCodeName ");
            strSql.Append(" where FCodeClsNo=@FCodeClsNo and FEntityNo=@FEntityNo and FCodeNo=@FCodeNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FCodeClsNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FCodeNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fCodeClsNo;
            parameters[1].Value = fEntityNo;
            parameters[2].Value = fCodeNo;

            ElingOA6_Admin_DSJCJ.Model.SysBasicCodeName model = new ElingOA6_Admin_DSJCJ.Model.SysBasicCodeName();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.FCodeClsNo = ds.Tables[0].Rows[0]["FCodeClsNo"].ToString();
                model.FEntityNo = ds.Tables[0].Rows[0]["FEntityNo"].ToString();
                model.FCodeNo = ds.Tables[0].Rows[0]["FCodeNo"].ToString();
                model.FCodeName = ds.Tables[0].Rows[0]["FCodeName"].ToString();
                model.FDataType = ds.Tables[0].Rows[0]["FDataType"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FCodeClsNo,FEntityNo,FCodeNo,FCodeName,FDataType ");
            strSql.Append(" FROM t_SysBasicCodeName ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        /// <param name="top"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <returns></returns>
        public DataSet GetList(int top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (top > 0)
            {
                strSql.Append(" top " + top.ToString());
            }
            strSql.Append(" FCodeClsNo,FEntityNo,FCodeNo,FCodeName,FDataType ");
            strSql.Append(" FROM t_SysBasicCodeName ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_SysBasicCodeName";
            parameters[1].Value = "FCodeNo";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method

        public bool ExistsFtpTypeCodeNo(string FFTPType)
        {

            StringBuilder strSql = new StringBuilder();
             strSql.Append("SELECT COUNT(1) FROM dbo.t_FTP WHERE FFTPType=@FFTPType");
                 SqlParameter[] parameters= {
                                                new SqlParameter("@FFTPType",SqlDbType.NVarChar,8)

                                            };

                 parameters[0].Value = FFTPType;
                 return DbHelperSQL.Exists(strSql.ToString(), parameters);

        }
        #region 增加的方法

        /// <summary>
        /// 获取数据列表 部分字段 FCodeNo,FCodeName,FDataType 根据FDataType降序
        /// </summary>
        /// <param name="clsNo"></param>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public DataSet GetListByClsNo(string clsNo, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FCodeNo,FCodeName,FDataType ");
            strSql.Append(" FROM t_SysBasicCodeName  where FCodeClsNo=@FCodeClsNo and FEntityNo=@FEntityNo");
            strSql.Append(" order by FDataType desc");

            SqlParameter[] parameters = {
                    new SqlParameter("@FEntityNo",SqlDbType.NVarChar,15),
                    new SqlParameter("@FCodeClsNo",SqlDbType.NVarChar,8)};

            parameters[0].Value = entityNo;
            parameters[1].Value = clsNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        #endregion

    }
}

